﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Afupdate, $stateParams,GoDialog,GoPage) {
		$scope.order_id = $stateParams.order_id;
		//$scope.createtime = $stateParams.createtime;
		$scope.input = new Object();
		$scope.input.sum = new Array();

		$scope.get_init = function() {
			for(var i = 0; i < $scope.datas.order.length; i++) {
				$scope.datas.order[i].now_total = 0;
				for(var n = 0; n < $scope.datas.order[i].goods.length; n++) {
					$scope.datas.order[i].now_total += $scope.datas.order[i].goods[n].price;
				}
				$scope.input.sum.push(parseFloat($scope.datas.order[i].now_total)+parseFloat($scope.datas.order[i].cost_shipping));
			}

		}
		$scope.get_order = function(){
			order = $scope.datas.order;
		}
		$scope.submit = function(index) {
		    if($scope.datas.order[index].cost_shipping==null){
		        $scope.datas.order[index].cost_shipping=0;
		    }
		    if($scope.datas.order[index].cost_shipping<0){
		        GoDialog.toast("配运费不能为负值！");
		        return;
		    }
		Afupdate.af_change($scope.order_id,parseFloat($scope.datas.order[index].cost_shipping),function(data){
		    if(data.success){
                GoDialog.toast("修改成功！");
                GoPage.go_states("afallseller");
		    }else{
		        GoDialog.toast(data.msg);
		    }

		})
		}
		$scope.init_data = function() {
			Afupdate.af_update($scope.order_id, function(data) {
				$scope.datas = data;
				$scope.get_init();
			})
		}
		$scope.init_data();

		//加
		$scope.jia=function(index){
		    if(!$scope.datas.order[index].cost_shipping){
		        $scope.input.sum[index]=$scope.datas.order[index].now_total;
		    }else{
		        $scope.input.sum[index]=parseFloat($scope.datas.order[index].now_total)+parseFloat($scope.datas.order[index].cost_shipping);
		    }
		}
	}

	controller.$inject = ['$scope', 'Afupdate', '$stateParams','GoDialog','GoPage'];

	return app.registerController("afupdatectrl",controller);
});